﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace YahooMoviesStormer
{
    public class Storage
    {
        static string SettingsFile = "config.xml";

        public static List<string> Accounts { get; set; }
        public static string ReviewURL { get; set; }

        public class StorageInstance
        {
            public List<string> Accounts { get; set; }
            public string ReviewURL { get; set; }
        }

        public static void Save()
        {
            var instance = new StorageInstance();
            instance.Accounts = Accounts;
            instance.ReviewURL = ReviewURL;

            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(instance.GetType());
            using (var stream = new FileStream(SettingsFile, FileMode.Create))
            {
                x.Serialize(stream, instance);
            }
        }

        public static void Load()
        {
            try
            {
                var instance = new StorageInstance();
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(instance.GetType());
                using (var stream = new FileStream(SettingsFile, FileMode.OpenOrCreate))
                {
                    instance = x.Deserialize(stream) as StorageInstance;
                }
                Accounts = instance.Accounts;
                ReviewURL = instance.ReviewURL;
            }
            catch
            {
                Accounts = new List<string>();
                ReviewURL = "";
            }
        }
    }
}
